<?php

namespace App;

use App\Models\Department;
use App\Models\NewRole;
use App\Models\SaleAccount;
use App\Models\UserRoleAuth;
use App\Models\UsersMarket;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use Notifiable;
    use \App\Traits\HasPermissions;

    protected $table = 'yt_new_users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
//    protected $fillable = ['name', 'email', 'password', 'username',];


    protected $fillable = [
        'name',
        'no', // 工号
        'dept_id', // 部门id
        'class_id',
        'email',
        'phone',
        'status',
        'ips',
        'log_city', // 登入地区
        'password',
        'creator',
        'is_single_login', // 是否单设备登录
        'is_use', // 是否创建账号
    ];

    // 职级
    const JobClass = [
        self::JobClass_ge_manager => '总经理',
        self::JobClass_manager => '经理',
        3 => '主管',
        4 => '组长',
        5 => '员工',
    ];

    const JobClass_ge_manager = 1; // 总经理
    const JobClass_manager = 2; //经理


    const Status_active = 1;
    // 在职状况
    const Status = [
        self::Status_active => '在职',
        2 => '离职',
    ];

    protected function username()
    {
        return 'no';
    }

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];


    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }


    // 是否有超管角色
    static function isAdmin(self $user=null)
    {
        if ($user) {
            return $user->isAdministrator();
        } else {
            return request()->user()->isAdministrator();
        }
    }

    public function roles()
    {
        $pivotTable = config('admin.database.role_users_table');
        return $this->belongsToMany(NewRole::class, $pivotTable, 'user_id','role_id');
    }


    // 用户菜单权限
    static function userPermisMenu($user)
    {
        // 用户所有角色
        $roles = $user->roles;
        $checkMenus = [];
        foreach($roles as  $role) {
//            $role->all_auth && $allPerms=true;
            $checkMenus = array_merge($checkMenus, $role->menus->pluck('id')->toArray());
        }
        // 用户角色权限表的数据
        $per = UserRoleAuth::where('user_id', $user->id)->first();
        // 用户“角色”的菜单 和 用户“角色权限”的菜单 合并
        if ($per) {
            $checkMenus = array_diff($checkMenus, explode(',',$per->decreased));
            $checkMenus = array_merge($checkMenus,explode(',',$per->increased));
        }
        return $checkMenus;
    }


    public function saleAccounts(): BelongsToMany
    {
        return $this->belongsToMany(SaleAccount::class,
            'yt_users_has_accounts',
            'user_id', 'account_id');
    }


    public function department()
    {
        return $this->belongsTo(Department::class, 'dept_id','dept_id');
    }

    public function market()
    {
        return $this->hasMany(UsersMarket::class, 'user_id', 'id');
    }

    static function getJobClassId($name) {
        return  array_search($name,self::JobClass);
    }

    static function getStatusId($name) {
        return  array_search($name,self::Status);
    }

}
